

// 113.路径总和II
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        // DFS每个位置有选和不选两种情况
        vector<vector<int>> ret;
        vector<int> tmp;
        function<void(TreeNode* , int)> dfs = [&](TreeNode* pos ,int sum)
        {
            sum += pos->val;
            tmp.push_back(pos->val);
            if(pos->left == nullptr && pos->right == nullptr && sum == targetSum)
            {  
                ret.push_back(tmp);
                tmp.pop_back();
                return ;
            }
            if(pos->left) dfs(pos->left , sum);
            if(pos->right) dfs(pos->right , sum);
            tmp.pop_back();
        };
        if(root) dfs(root , 0);
        return ret;
    }
};